package com.yhd.prototype;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yhd
 * @email yinhuidong1@xiaomi.com
 * @description TODO
 * @since 2021/5/22 下午4:54
 */
public class MyBatis {

    private Map<String, Person> cache = new HashMap<>();


    public Person getFromDb(String name) throws CloneNotSupportedException {
        Person person = new Person().setName(name).setAge(20);
        System.out.println("getFromDb--------" + person.toString());
        return person.clone();
    }

    public Person getPerson(String name) throws Exception{
        if (cache.containsKey(name)){
            Person person = cache.get(name);
            System.out.println("getPerson ----------"+person.toString());
            return person;
        }else{
            Person db = getFromDb(name).clone();
            cache.put(db.getName(),db);
            return db.clone();
        }
    }
}
